import Axios from 'axios'
import {ElNotification} from "element-plus";

const Service = Axios.create({
    baseURL: '/api',
    timeout: 20000,
    withCredentials: true,
    headers: {
        'Content-Type': 'application/json'
    }
})

Service.defaults.baseURL = '/api'

Service.defaults.timeout = 20000

//允许跨域cookie
Service.defaults.withCredentials = true

// 添加请求拦截器
Service.interceptors.request.use(config => {
        console.log("进入请求拦截器")
        console.log(config)

        return config
    },
    function (error) {
        console.log("请求错误：")
        console.log(error)
        return Promise.reject(error)
    }
)

// 添加响应拦截器
Service.interceptors.response.use(
    function (response) {

        console.log("进入响应拦截器:")
        console.log(response)

        if (response.data.code && response.data.code !== 200) {
            ElNotification({
                title: "错误",
                message: response.data.msg,
                type: 'error',
                duration: 2000
            })
        }
        return response
    },
    function (error) {
        console.log("响应错误：")
        console.log(error)
        return Promise.reject(error)
    }
)
export default Service
